package com.sibu.yx.promotion.admin.web.support.filter;


import com.doubo.common.util.StringUtil;
import com.doubo.json.util.JsonUtil;
import com.sibu.yx.promotion.common.enums.ErrorCodeEnum;
import com.sibu.yx.promotion.common.manage.ResponseManage;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ThreadContext;
import org.apache.shiro.web.filter.AccessControlFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author     ：yuwenbo
 * @date       ：Created in 2019/4/15 15:56
 * @description：本地拦戴url
 */
@Deprecated
public class CustomAuthFilter extends AccessControlFilter {

    /**
     * 验证是否有登陆信息
     * @param servletRequest
     * @param servletResponse
     * @param o
     * @return
     * @throws Exception
     */
    @Override
    protected boolean isAccessAllowed(ServletRequest servletRequest, ServletResponse servletResponse, Object o) throws Exception {
        Subject subject=super.getSubject(servletRequest,servletResponse);
        return !StringUtil.isNull(subject)&&subject.isAuthenticated();
    }

    @Override
    protected boolean onAccessDenied(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception {
        servletResponse.setContentType("application/json;charset=UTF-8");
        servletResponse.setCharacterEncoding("utf-8");
        servletResponse.getWriter().write(JsonUtil.toJson(ResponseManage.fail(ErrorCodeEnum.SESSION_TIMEOUT)));
        return false;
    }


    @Override
    public  void doFilterInternal(ServletRequest request, ServletResponse response, FilterChain chain)
            throws ServletException, IOException{
        TokenSubject tokenUidSubject=new TokenSubject((HttpServletRequest)request);
        ThreadContext.bind(tokenUidSubject);
        super.doFilterInternal(request,  response,  chain);
    }


}
